//
//  ServerRequestUtility.m
//  OASystemManage
//
//  Created by joker on 15/7/14.
//  Copyright (c) 2015年 刘宣佚. All rights reserved.
//
#import "ServerRequestUtility.h"
#import <CommonCrypto/CommonDigest.h>
#import <UIKit/UIKit.h>

//    //服务器地址
//#define TestServerSite       (@"https://192.168.1.24")
//#define FormalServerSite    (@"https://service.oa.jiuan-roa.com")
//#define FormalServerSite2  (@"https://oa.jiuan.com:4433")
//
//    //API地址
//#define LogInAPI    (@"/MobileOfficeAutomationAPI/LoginWeb.ashx")
//#define YearHolidayAPI  (@"/MobileOfficeAutomationAPI/YearHolidayWeb.ashx")
//#define SpecialAPI  (@"/MobileOfficeAutomationAPI/YearHolidayWeb.ashx")
//#define CheckAccessKeyAPI  (@"/MobileOfficeAutomationAPI/CheckAccessKeyWeb.ashx")
//
//    NSString *  const ServerSite = FormalServerSite2;//这个变量方便测试地址也正式地址的切换
//
////NSString* const ServerSite = TestServerSite;
//    //请求地址
//NSString * const LoginWeb = @"/MobileOfficeAutomationAPI/LoginWeb.ashx";//登录
//NSString * const YearHolidayWeb = @"/MobileOfficeAutomationAPI/YearHolidayWeb.ashx";//年假信息查询
//NSString * const SpecialWeb = @"/MobileOfficeAutomationAPI/SpecialWeb.ashx";//考勤异常查询
//NSString * const CheckAccessKey = @"/MobileOfficeAutomationAPI/CheckAccessKeyWeb.ashx";


    ////////////////////////////////////////////////////

@implementation ServerRequestUtility
#pragma mark - private functions
//+(NSString *)startRequestToServer: (NSString *) server API:(NSString *)path WithDictionary: (NSDictionary *)requestParams{
//    NSURL * serverURL = [NSURL URLWithString:server];
//    NSURL *requestURL = [NSURL URLWithString:path
//                               relativeToURL:serverURL];
//        //MARA: 使用同步请求，在正式地址上可用，测试地址会报证书验证错误
//    NSMutableURLRequest * request = [NSMutableURLRequest  requestWithURL:requestURL];
//    [request setHTTPMethod:@"POST"];
//    
//    NSError * error;
//    NSData *paramsData = [NSJSONSerialization dataWithJSONObject:requestParams options:NSJSONWritingPrettyPrinted error:&error];
//    
//    NSString *paramsString =[NSString stringWithFormat:@"content=%@", [[NSString alloc] initWithData:paramsData encoding:NSUTF8StringEncoding]];
//    
//    [request setHTTPBody:[paramsString dataUsingEncoding:NSUTF8StringEncoding]];
//    
//    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
//    
//    if(returnData){
//        NSDictionary * returnDict = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableLeaves error:nil];
//        
//        long int resultCode = [returnDict[@"Result"] integerValue];
//        NSString *result = [NSString stringWithFormat:@"%ld",resultCode];
//        NSString *resultMessage = [NSString stringWithFormat:@"%@", returnDict[@"ResultMessage"]];
//#ifdef DUMP_INFO
//        NSLog(@"%@",resultMessage);
//#endif
//        if([result isEqualToString:@"1"]) {
//            if([resultMessage isEqualToString:@"100"])
//            {
//                return returnDict[@"ReturnValue"];
//            }
//        }else{
//            NSString *title = [self showResult:result];
//            NSString *body = [self showResultMessage:resultMessage];
//            [self showAlertWithTitle:title MessageBody:body];
//        }
//    }
//    else
//    {
//        [self showAlertWithTitle:@"Request Error" MessageBody: error.userInfo[@"NSLocalizedDescription"]];
//    }
//    return nil;
//}


+(NSString *)getCurrentTimeIntervalSince1970 {
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSince1970];
     return [NSString stringWithFormat:@"%ld",(long int)timeInterval];
}

#pragma  mark - public functions
//+(NSString *) LoadAbnormalAttendanceWith:(NSString *) OAfUserID AccessKey:(NSString *)accessKey{
//    NSDictionary *params = @{
//                             @"OAfUserID":OAfUserID,
//                             @"AccessKey": accessKey,
//                             @"DateTime":[self getCurrentTimeIntervalSince1970]
//                             };
//    return [self startRequestToServer:ServerSite API:SpecialWeb WithDictionary:params];
//}
//+(NSString *) LoadStatutoryAnnualLeaveWith:(NSString*)AccessKey  OAfUserID: (NSString*)userID {
//
//    NSDictionary *params = @{
//                             @"AccessKey":AccessKey,
//                             @"DateTime":[self getCurrentTimeIntervalSince1970],
//                             @"OAfUserID":userID
//                             };
//    
//    return [self startRequestToServer:ServerSite API:YearHolidayWeb WithDictionary:params];
//}
//+(NSString *) LoginServerWith:(NSString *)userName PassWord: (NSString *) password{
//    NSString * md5password = [self md5:password];
//    NSDictionary *inputDictionary = @{
//                                      @"LogonName":userName,
//                                      @"PassWord":md5password
//                                      };
//    return [self startRequestToServer:ServerSite API:LoginWeb WithDictionary:inputDictionary];
//}

+(void)showAlertWithTitle:(NSString *) alertTitle  MessageBody: (NSString *)body {
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:alertTitle
                          message:body
                          delegate:self
                          cancelButtonTitle:@"Cancel"
                          otherButtonTitles:@"Ok",nil];
    
    [alert show];
}
+(NSString *)showResult: (NSString *) result{
    NSDictionary * Result =@{
                             @"1":@"成功",
                             @"2":@"失败",
                             @"3":@"错误"};
    return Result[result];
}
+(NSString *)showResultMessage:(NSString *)resultMessage{
    NSDictionary * ResultMessage = @{
                                     @"100":@"成功",
                                     @"201":@"用户名或密码错误",
                                     @"201.3":@"用户名不可以为空",
                                     @"201.4":@"密码不可以为空",
                                     @"202":@"用户已离职",
                                     @"210.2":@"AccessKey过期",
                                     @"230":@"上传数据不完整，或格式不正确",
                                     @"500":@"服务器内部错误",
                                     @"999":@"未知错误"
                                     };
    return ResultMessage[resultMessage];
}
+(NSString *)md5: (NSString *) sourceStr{
    
    const char *cStr = [sourceStr cStringUsingEncoding:NSUTF16StringEncoding];
    unsigned char result[16];
    CC_MD5( cStr, (CC_LONG)[sourceStr length] * 2, result );
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}
@end
